import scrapy
import json
from image_spider.items import ImageSpiderItem


class ImageSpider(scrapy.Spider):
    name = "image"
    allowed_domains = ["duitang.com"]
    url = "https://www.duitang.com/napi/blogv2/list/by_search/?kw=%E7%BE%8E%E5%A5%B3&after_id={}&type=feed&include_fields=top_comments%2Cis_root%2Csource_link%2Citem%2Cbuyable%2Croot_id%2Cstatus%2Clike_count%2Clike_id%2Csender%2Calbum%2Creply_count%2Cfavorite_blog_id&_type=&_=1696420988093"
    start_urls = [url.format(0)]

    def parse(self, response, **kwargs):
        for num in range(0, 100):
            new_url = self.url.format(num * 24)
            yield scrapy.Request(url=new_url, callback=self.parse_image_data)

    def parse_image_data(self, response):
        dict_data = json.loads(response.text)["data"]["object_list"]
        for data in dict_data:
            item = ImageSpiderItem()
            item["href"] = data["photo"]["path"]
            yield item
